<?php
namespace Home\Controller;
use Home\Controller\BaseController;
class WxhelperController extends BaseController{
    public function trimString($value){
        $ret=null;
        if($value != null){
            $ret=$value;
            if(strlen($ret)==0){
                $ret=null;
            }
        }
        return $ret;
    }
    /**
     * 生成随机字符串
     */
    public function getNoncestr($length=32){
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str ="";
        for ( $i = 0; $i < $length; $i++ )  {
            $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
        }
        return $str;
    }
    /**
     * 格式化签名参数
     */
    public function formatBizQueryParaMap($paramap,$urlencode){
        $buff='';
        ksort($paramap);
        foreach ($paramap as $k=>$v){
            if($urlencode){
                $v=urldecode($v);
            }
            $buff.= $k . '=' . $v . '&';
        }
        $reqpar='';
        if(strlen($buff)>0){
            $reqpar=substr($buff, 0 ,strlen($buff)-1);
        }
        return $reqpar;
    }
    /**
     * 数组转xml
     */
    public function arrayToxml($arr){
        $xml="<xml>";
        foreach ($arr as $k=>$v){
            $xml.="<".$k."><![CDATA[".$v."]]></".$k.">";
        }
        $xml.="</xml>";
        return $xml;
    }
    /**
     * xml转数组
     */
    public function xmlToarray($xml){
        if(!$xml) E('xml数据异常');
        //禁止引用外部的xml数据实体
        libxml_disable_entity_loader(true);
        return json_decode(json_encode(simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA)),true);
    }
    
    
    
}